package com.azure.service;

import com.azure.entity.TActivity;
import com.azure.mapper.bigstore.TActivityMapper;
import com.azure.service.Report.CategoryReportService;
import com.azure.service.Report.ProductReportService;
import com.azure.service.Report.TurnoverReportService;
import com.azure.service.Report.UserReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * Created by liuhuaibin on 2018/3/13.
 */
@Component
public class JobService {
    @Autowired
    private UserReportService userReportService;
    @Autowired
    private CategoryReportService categoryReportService;
    @Autowired
    private ProductReportService productReportService;
    @Autowired
    private TurnoverReportService turnoverReportService;

    @Autowired
    private TActivityMapper tActivityMapper;
    /**
     * 每日定时任务   每天0点15分执行
     */
    //@Scheduled(cron = "0 05 17 * * ?")
    public void dailyReport(){
        LocalDate date = LocalDate.now().minusDays(1);
        userReportService.totalDailyReport(date);
        categoryReportService.totalDailyReport(date);
        productReportService.totalDailyReport(date);
        turnoverReportService.totalDailyReport(date);
    }
    /**
     * 每月定时任务   每月1号凌晨3点执行
     */
    //@Scheduled(cron = "0 0 3 1 * ?")
    public void monthlyReport(){
        LocalDate date = LocalDate.now().minusMonths(1);
        LocalDate minusMonthDate = LocalDate.now().minusMonths(2);
        LocalDate startDate = LocalDate.of(minusMonthDate.getYear(),minusMonthDate.getMonthValue(),
                                                    minusMonthDate.lengthOfMonth());
        LocalDate stopDate = LocalDate.of(date.getYear(),date.getMonthValue(),
                                                date.lengthOfMonth()-1);
        userReportService.totalMonthlyReport(startDate,stopDate);
        categoryReportService.totalMonthlyReport(startDate,stopDate);
        productReportService.totalMonthlyReport(startDate,stopDate);
        turnoverReportService.totalMonthlyReport(startDate,stopDate);
    }
    //@Scheduled(cron = "0 11 18 * * ?")
    public void test(){
        TActivity tActivity = tActivityMapper.selectByPrimaryKey(1L);
        System.out.println("activity:"+tActivity);
    }
}
